package mode;

import java.awt.Color;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Random;

public class ShapeFactory {
	private static int[][][] shapes = 
		  { { { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 } },
            { { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
            { { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
            { { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
            { { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
            { { 0, 0, 0, 0 }, { 0, 0, 1, 1 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
            { { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 } } };
    private static Color[] color = { Color.red, Color.gray, Color.GREEN, Color.cyan, Color.yellow, Color.magenta,Color.orange };
	public static final int NODEWIDTH = 20;
	public static final int NODEHEIGHT = 20;
	
	public ShapeFactory() {
		// TODO Auto-generated constructor stub
	}
	
	public static Shape createShape() throws ClassNotFoundException, IOException{
		Shape shape=new Shape();
		int index=new Random().nextInt(shapes.length);
		int[][][] x=(int[][][]) deepClone(shapes);
		System.out.println(x);
		for(int i=0;i<1;i++){
			for(int j=0;j<x[0].length;j++){
				for(int k=0;k<x[0][j].length;k++){
					System.out.print(x[0][j][k]+"\t");
				}
			}
		}
		shape.setShape(x[0]);
		shape.setColor(color[index]);
		return shape;
	}
	
	//深克隆
	public static Object deepClone(int[][][] shapes) throws IOException, ClassNotFoundException{
		ByteArrayOutputStream bos=new ByteArrayOutputStream();
		ObjectOutputStream oos=new ObjectOutputStream(bos);
		oos.writeObject(shapes);
		
		ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
		ObjectInputStream ois=new ObjectInputStream(bis);
		return ois.readObject();
	}
}
